package com.cqeec.waimaidemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqeec.waimaidemo.common.CustomException;
import com.cqeec.waimaidemo.entity.Category;
import com.cqeec.waimaidemo.entity.Dish;
import com.cqeec.waimaidemo.entity.Setmeal;
import com.cqeec.waimaidemo.mapper.CategoryMapper;
import com.cqeec.waimaidemo.service.CategoryService;
import com.cqeec.waimaidemo.service.DishService;
import com.cqeec.waimaidemo.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends
        ServiceImpl<CategoryMapper, Category>
        implements CategoryService {
    /**
     * @param id
     */
    @Autowired
    SetmealService setmealService;
    @Autowired
    DishService dishService;
    @Override
    public void deleteById(Long id) {
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(wrapper);
        if(count>0){
            throw new CustomException("当前分类关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getCategoryId,id);
        int count1=setmealService.count(wrapper1);
        if (count1>0){
            throw  new CustomException("当前分类关联了套餐，不能删除！！！");
        }
        this.removeById(id);
    }
}
